﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Xml.Linq;
using webApi;

namespace YYS.Controllers
{
    public class fileController : ControlerEx
    {        
        [HttpPost]
        [v3Authorize()]
        public JsonResult getSQLScriptList()
        {
            var fileName = Request.Form["fileName"].xTrim();
            // 设置要列出文件的文件夹路径
            string folderPath = YYS.API.getHostPath("~/SQL Script");
            var list = new List<object>();
            try
            {
                // 获取指定文件夹中的所有文件，包括子文件夹中的文件
                string[] fileEntries = Directory.GetFiles(folderPath, "*.*", SearchOption.AllDirectories);
                foreach (string filePath in fileEntries)
                {
                    FileInfo file = new FileInfo(filePath);
                    if (file.Length == 0) continue;
                    if (!file.Name.Contains(fileName)) continue;
                    list.Add(new
                    {
                        fileName = file.Name,//文件名称
                        fileFullName = file.FullName,//完整路径
                        fileCreationTime = file.CreationTime.xFormatDateTime("yyyy-MM-dd HH:mm:ss"),//创建时间
                        fileLastAccessTime = file.LastAccessTime.xFormatDateTime("yyyy-MM-dd HH:mm:ss"),//最后访问时间
                        fileLastWriteTime = file.LastWriteTime.xFormatDateTime("yyyy-MM-dd HH:mm:ss"),//修改时间
                        fileLength = GetFileSize(file.Length)//文件大小: 
                    });
                }                
            }
            catch (Exception ex)
            {
                return backJson(1, ex.Message);
            }
            return backJson(0,"", list);
        }

        [HttpPost]
        [v3Authorize()]
        public JsonResult openFile()
        {
            var fileFullName = Request.Form["fileFullName"].xTrim();
            string logContent = "";
            try
            {
                logContent = System.IO.File.ReadAllText(@fileFullName, Encoding.GetEncoding("gb2312"));
                //System.Diagnostics.Process.Start(@"F:\vue脚手架项目\小杨工具库\VUE-API\WindowsFormsApp\bin\Release\WindowsFormsApp.exe");
            }
            catch (Exception ex)
            {
                return backJson(1, ex.Message);
            }
            return backJson(0, "", logContent);
        }
    }
}